#!/usr/bin/env node

/**
 * 这个脚本用于启动Vite开发服务器，并过滤掉所有与Sass @import废弃相关的警告
 */

const { spawn } = require("child_process");
const os = require("os");

// 确定当前操作系统
const isWin = os.platform() === "win32";

// 设置环境变量
process.env.SASS_SILENCE_DEPRECATION_WARNINGS = "1";
process.env.NODE_OPTIONS = "--max-old-space-size=4096";

// 创建Vite进程
const command = isWin ? "vite.cmd" : "vite";
const vite = spawn(command, [], {
  stdio: ["inherit", "pipe", "pipe"], // 将stdout和stderr重定向到管道
  shell: true,
  env: process.env
});

// 要过滤的关键词
const warningsToFilter = [
  "Deprecation Warning [import]",
  "Sass @import rules are deprecated",
  "More info and automated migrator:"
];

// 处理标准输出
vite.stdout.on("data", data => {
  const output = data.toString();

  // 检查是否包含需要过滤的关键词
  if (!warningsToFilter.some(warning => output.includes(warning))) {
    process.stdout.write(output);
  }
});

// 处理标准错误
vite.stderr.on("data", data => {
  const output = data.toString();

  // 检查是否包含需要过滤的关键词
  if (!warningsToFilter.some(warning => output.includes(warning))) {
    process.stderr.write(output);
  }
});

// 处理进程退出
vite.on("close", code => {
  process.exit(code);
});

// 处理Ctrl+C信号
process.on("SIGINT", () => {
  vite.kill("SIGINT");
});

console.log("\n🚀 启动Vite服务器，并过滤Sass废弃警告\n");
